// 输入: [1,7,4,9,2,5]
// 输出: 6 
// 解释: 整个序列均为摆动序列。
// function wingMaxlength(arr) {
//     if (arr.length < 2) {
//         return arr.length
//     }
//     let prediff = arr[0] - arr[1];
//     let count = prediff === 0 ? 1 : 2
//     for (let i = 0; i < arr.length; i++) {
//         let diff = arr[0] - arr[1];
//         if ((prediff <= 0 && diff > 0) || diff < 0 && prediff >= 0) {
//             count++;
//             prediff = diff
//         }
//     }
//     return count
// }


function wingMaxlength(arr) {
    if (arr.length < 2) {
        return arr.length
    }
    let prediff = arr[0] - arr[1];
    let count = prediff === 0 ? 1 : 2; // 看看是否存在 [1,1] 这种情况
    for (let i = 2; i < arr.length; i++) {
        let diff = arr[i] - arr[i - 1] // 比较2-1
        // 大小 大小才是摆动
        if ((prediff >= 0 && diff < 0) || (prediff <= 0 && diff > 0)) {
            count+=1;
            prediff = diff;
        }
    }
    return count
}
console.log(wingMaxlength([1,7,4,9,2,5]))